package com.anolesoft.amljmes.fourteenDispatch.controller;


import com.anolesoft.amljmes.fourteenDispatch.dto.ChuangTouJiHuaEmpDTO;
import com.anolesoft.amljmes.fourteenDispatch.dto.DemoVO;
import com.anolesoft.amljmes.fourteenDispatch.dto.EmpDTO;
import com.anolesoft.amljmes.fourteenDispatch.dto.MesPlanDispatchingDto;
import com.anolesoft.amljmes.fourteenDispatch.service.info.MesPlanDispatchingService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.SecurityUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value="/mesPlanDispatch")
@Slf4j
public class MesPlanDispatchingController {
    @Autowired
    MesPlanDispatchingService mesPlanDispatchingService;

    //根据制造记录查询
    @PostMapping(value="/queryPacrNo",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult DispatchingSelectAll(@RequestBody Map<String, String> map) {
        String pacrNo = map.get("pacrNo");
        System.out.println("pacrNo"+pacrNo);
        List<MesPlanDispatchingDto> list=mesPlanDispatchingService.DispatchingSelectAll(pacrNo);
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //批量清除
    @PostMapping(value="/deleteDispatching", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult deleteDispatching(@RequestBody String[] objuids){
        System.out.println(">>>>>>delete");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesPlanDispatchingService.deleteDispatching(asList));
    }

    //批量取消派工
    @PostMapping(value="/updateDispatching", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateDispatch(@RequestBody  String[] objuids){
        System.out.println(">>>>>>update");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesPlanDispatchingService.updateDispatching(asList));
    }

    @PostMapping(value="/query",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult orderSelectAll(@RequestBody Map<String, String> map) {
        String pacrNo = map.get("pacrNo");
        System.out.println("pacrNo"+pacrNo);
        List<MesPlanDispatchingDto> list=mesPlanDispatchingService.orderSelectAll(pacrNo);
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    @PutMapping(value = "/addDispatching", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult addDispatching(@RequestBody Map<String,Object> inFoDtoParam) {
        List<Map<String,Object>> selectedRows = (List<Map<String,Object>>)inFoDtoParam.get("selectedRows");
        List<MesPlanDispatchingDto> list = new Gson().fromJson(new Gson().toJson(selectedRows), new TypeToken<List<MesPlanDispatchingDto>>(){}.getType());
        String checktime = (String)inFoDtoParam.get("checktime");
        String caozuozhe = (String)inFoDtoParam.get("caozuozhe");
        System.out.println(list);
        try{
            int a=mesPlanDispatchingService.addDispatching(list,checktime,caozuozhe);
            return new AjaxResult(HttpStatus.SUCCESS,Constants.MODIFY_SUCCESS_MESSAGE,a);
        }catch (RuntimeException e1){
            e1.printStackTrace();
            return new AjaxResult(HttpStatus.SUCCESS,Constants.MODIFY_SUCCESS_MESSAGE,"人员不存在");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(HttpStatus.SUCCESS,Constants.MODIFY_SUCCESS_MESSAGE,"添加失败");
        }
    }

    @PostMapping(value="/queryEmpEid",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryEmpEid(){
        List<ChuangTouJiHuaEmpDTO> chuangTouJiHuaEmpDTOS =mesPlanDispatchingService.queryEmpEid();
        AjaxResult success = AjaxResult.success();
        success.put("data", chuangTouJiHuaEmpDTOS);
        return success;
    }
    //    @PostMapping(value="/queryEmpEid",produces= MediaType.APPLICATION_JSON_VALUE)
//    public AjaxResult queryEmpEid() {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        String obj=loginUser.getEmpUid();
//        System.out.println("obj"+obj);
//        ChuangTouJiHuaEmpDTO fenguangYingduEmpDTO=mesPlanDispatchingService.queryEmpEid(obj);
//        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,fenguangYingduEmpDTO);
//    }

    @PostMapping(value="/queryById",produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryById() {
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String objuid = loginUser.getEmpUid();
        System.out.println(">>>>>>queryById"+objuid);
        EmpDTO empDTO = mesPlanDispatchingService.queryById(objuid);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,empDTO);
    }

    @PostMapping(value="/updateprocessstate", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateprocessstate(@RequestBody String[] objuids){
        System.out.println(">>>>>>updateprocessstate");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesPlanDispatchingService.updateprocessstate(asList));
    }

    @PostMapping(value = "/queryForJiedan")
    public AjaxResult queryForJiedan(@RequestBody Map<String, String> map){
        String pacrNo = map.get("pacrNo");
        System.out.println("pacrNo"+pacrNo);
        String factoryId = pacrNo.substring(0,2);
        System.out.println("factory_id：" + factoryId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String fac_code = loginUser.getFacCode();
        String jiedanren = loginUser.getEmpUid();
        System.out.println("fac_code：" + fac_code);
        System.out.println("jiedanren：" + jiedanren);
        if (!fac_code.equals(factoryId)) {
            return AjaxResult.success("factoryId");
        }
        try {
            List<MesPlanDispatchingDto> result = this.mesPlanDispatchingService.queryForJiedan(pacrNo, fac_code, jiedanren);
            return AjaxResult.success("ok",result);
        } catch (RuntimeException e1){
            e1.printStackTrace();
            return AjaxResult.success("\"没有读取到考核日期，无法计算计划开工时间！");
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.success("查询发生异常");
        }
    }

    //@PutMapping(value = "/jiedan", produces = MediaType.APPLICATION_JSON_VALUE)
    //public AjaxResult jiedan(@RequestBody Map<String,Object> inFoDtoParam) {
    //    List<Map<String,Object>> selectedRows = (List<Map<String,Object>>)inFoDtoParam.get("selectedRows");
    //    List<MesPlanDispatchingDto> jiedanList = new Gson().fromJson(new Gson().toJson(selectedRows), new TypeToken<List<MesPlanDispatchingDto>>(){}.getType());
    //    log.debug("jiedanList:",jiedanList);
    //    String checktime = (String)inFoDtoParam.get("checktime");
    //    String caozuozhe = (String)inFoDtoParam.get("caozuozhe");
    //    LoginUser loginUser = SecurityUtils.getLoginUser();
    //    String jiedanren = loginUser.getEmpUid();
    //    String factory_id = loginUser.getFacCode();
    //    System.out.println("接单人："+jiedanren+"厂代号："+factory_id);
    //    log.debug("jiedanList:{}", jiedanList);
    //    try{
    //        int a = this.mesPlanDispatchingService.jiedan(jiedanList,checktime,caozuozhe,jiedanren,factory_id);
    //        return AjaxResult.success(String.valueOf(a));
    //    }catch (RuntimeException e1){
    //        e1.printStackTrace();
    //        return AjaxResult.success("人员不存在");
    //    }catch (Exception e){
    //        e.printStackTrace();
    //        return AjaxResult.success("添加失败");
    //    }
    //}
    @PostMapping(value = "/jiedan")
    public AjaxResult jiedan(@RequestBody List<MesPlanDispatchingDto> jiedanList) {
//        List<MesPlanDispatchingDto> jiedanList = demoVO.getSelectedRows();
//        LocalDate checktime = demoVO.getChecktime();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String caozuozhe = loginUser.getEmpUid();
        String jiedanren = loginUser.getEmpUid();
        String factory_id = loginUser.getFacCode();
        //log.debug("demoVO.getSelectedRows:{}",jiedanList);
        //log.debug("demoVO.getCaozuozhe:"+caozuozhe);
        log.debug("接单人："+jiedanren+"厂代号："+factory_id);
        log.debug("选中数据：{}",jiedanList);
        try{
            int a = this.mesPlanDispatchingService.jiedan(jiedanList,caozuozhe,jiedanren,factory_id);
            return AjaxResult.success(String.valueOf(a));
        }catch (RuntimeException e1){
            e1.printStackTrace();
            return AjaxResult.success("人员不存在");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.success("接单失败");
        }
    }

    

}
